package com.amigo.online.provider.video.mongo.dao.impl;

import java.util.List;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.amigo.online.provider.video.mongo.dao.AdvDao;
import com.mongodb.BasicDBObject;
import com.mongodb.client.AggregateIterable;

@Service
public class AdvDaoImpl implements AdvDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public AggregateIterable<Document> findAroundVideo(String collection, Double[] point, int searchMaxdistance,
                                                       BasicDBObject query, List<BasicDBObject> pipeline) {
        BasicDBObject aggregate = new BasicDBObject("$geoNear",
                new BasicDBObject("near",
                        new BasicDBObject("type", "Polyon").append("coordinates", point))
                        .append("distanceField", "distance")
                        .append("query", query)
                        .append("maxDistance", searchMaxdistance).append("spherical", true));
        pipeline.add(0, aggregate);
        AggregateIterable<Document> iterators = mongoTemplate.getCollection(collection).aggregate(pipeline);
        return iterators;
    }
}
